package cn.iocoder.yudao.module.pickbook.service.book;

import cn.iocoder.yudao.module.pickbook.dal.dataobject.book.PickbookBookDO;
import cn.iocoder.yudao.module.pickbook.dal.dataobject.book.PickbookChapterDO;
import cn.iocoder.yudao.module.pickbook.dal.dataobject.series.PickbookSeriesDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.pickbook.dal.mysql.book.PickbookBookMapper;
import cn.iocoder.yudao.module.pickbook.dal.mysql.book.PickbookChapterMapper;
import cn.iocoder.yudao.module.pickbook.dal.mysql.series.PickbookSeriesMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

/**
 * 绘本 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
@Slf4j
public class PickbookBookServiceImpl implements PickbookBookService {

    @Resource
    private PickbookBookMapper bookMapper;

    @Resource
    private PickbookChapterMapper chapterMapper;

    @Resource
    private PickbookSeriesMapper seriesMapper;

    @Override
    public PageResult<PickbookBookDO> getBookList(String category, String ageRange, Long seriesId, 
                                                 Integer pageNo, Integer pageSize) {
        // 计算偏移量
        int offset = (pageNo - 1) * pageSize;
        
        // 查询总数
        long count = bookMapper.selectCountByConditions(category, ageRange, seriesId);
        
        // 查询列表
        List<PickbookBookDO> list = bookMapper.selectListByConditions(category, ageRange, seriesId, offset, pageSize);
        
        return new PageResult<>(list, count);
    }

    @Override
    public PickbookBookDO getBook(Long id) {
        return bookMapper.selectById(id);
    }

    @Override
    public List<PickbookChapterDO> getBookChapters(Long bookId) {
        return chapterMapper.selectByBookId(bookId);
    }

    @Override
    public PickbookChapterDO getChapter(Long id) {
        return chapterMapper.selectById(id);
    }

    @Override
    public List<PickbookSeriesDO> getSeriesList(String category, String ageRange) {
        return seriesMapper.selectByConditions(category, ageRange);
    }

    @Override
    public PickbookSeriesDO getSeries(Long id) {
        return seriesMapper.selectById(id);
    }

    @Override
    public PageResult<PickbookBookDO> searchBooks(String keyword, Integer pageNo, Integer pageSize) {
        // 计算偏移量
        int offset = (pageNo - 1) * pageSize;
        
        // 查询总数
        long count = bookMapper.selectCountByKeyword(keyword);
        
        // 查询列表
        List<PickbookBookDO> list = bookMapper.selectListByKeyword(keyword, offset, pageSize);
        
        return new PageResult<>(list, count);
    }
} 